package com.zzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.config.UserContext;
import com.zzx.entity.TProductType;
import com.zzx.mapper.TProductTypeMapper;
import com.zzx.service.TProductTypeService;
import com.zzx.utils.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author CD
 * @description 针对表【t_product_type】的数据库操作Service实现
 * @createDate 2024-11-29 15:18:32
 */
@Service
public class TProductTypeServiceImpl extends ServiceImpl<TProductTypeMapper, TProductType> implements TProductTypeService {
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TProductType param) {
        LambdaQueryWrapper<TProductType> wrapper = new LambdaQueryWrapper<>(TProductType.class);
        wrapper.eq(TProductType::getCompanyId, UserContext.getCompanyId());
        wrapper.eq(TProductType::getProductTypeName, param.getProductTypeName());
        if (ObjectUtils.isEmpty(param.getProductTypeId())) {
            param.setProductTypeId(IdUtil.getSnowflakeNextId());
        } else {
            wrapper.ne(TProductType::getProductTypeId, param.getProductTypeId());
            TProductType tProductType = this.getBaseMapper().selectById(param.getProductTypeId());
            EntityUtils.mergeNonNullProperties(tProductType, param);
        }
        List<TProductType> tProductTypeList = this.getBaseMapper().selectList(wrapper);
        if (!tProductTypeList.isEmpty()) {
            throw new RuntimeException("名称重复");
        }
        this.saveOrUpdate(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TProductType param) {
        this.removeById(param.getProductTypeId());
    }

    @Override
    public List selectBy(TProductType param) {
        LambdaQueryWrapper<TProductType> wrapper = new LambdaQueryWrapper<>(TProductType.class);
        if (!ObjectUtils.isEmpty(param.getProductTypeName())) {
            wrapper.like(TProductType::getProductTypeName, param.getProductTypeName());
        }
        if (!ObjectUtils.isEmpty(param.getProductTypeCode())) {
            wrapper.like(TProductType::getProductTypeCode, param.getProductTypeCode());
        }
        if (!ObjectUtils.isEmpty(param.getProductTypeId())) {
            wrapper.eq(TProductType::getProductTypeId, param.getProductTypeId());
        }
        wrapper.eq(TProductType::getCompanyId, UserContext.getCompanyId());
        List<TProductType> tProductTypes = this.getBaseMapper().selectList(wrapper);
        return this.generateTree(tProductTypes);
    }

    private List<Tree<String>> generateTree(List<TProductType> dataList) {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<String>> treeNodes = TreeUtil.build(dataList, "0", treeNodeConfig,
                (data, tree) -> {
                    tree.setId(String.valueOf(data.getProductTypeId()));
                    tree.setParentId(data.getParentId());
//                    tree.setName(data.getProductTypeName());
                    // 扩展属性 ...

                    tree.putExtra("productTypeId", data.getProductTypeId());
                    tree.putExtra("productTypeName", data.getProductTypeName());
                    tree.putExtra("productTypeCode", data.getProductTypeCode());
                });
        return treeNodes;
    }
}




